#include "common.h"

/**
 * LCR 179. 查找总价格为目标值的两个商品
简单
购物车内的商品价格按照升序记录于数组 price。请在购物车中找到两个商品的价格总和刚好是 target。若存在多种情况，返回任一结果即可。
 */
class Solution
{
public:
    std::vector<int> twoSum(std::vector<int> &price, int target)
    {
        int left = 0;
        int right = price.size() - 1;
        while (left < right)
        {
            if (price[left] + price[right] < target)
            {
                ++left;
            }
            else if (price[left] + price[right] > target)
            {
                --right;
            }
            else
            {
                return std::vector<int>({price[left], price[right]});
            }
        }
        return std::vector<int>();
    }
};

class Solution
{
public:
    vector<int> twoSum(vector<int> &price, int target)
    {
        int left = 0, right = price.size() - 1;
        while (left < right)
        {
            int sum = price[left] + price[right];
            if (sum > target)
                right--;
            else if (sum < target)
                left++;
            else
                return {price[left], price[right]};
        }
        return {-1, -1};
    }
};